<?php

namespace app\api\model;

use app\common\model\BaseModel;
use think\facade\Db;
use think\model\concern\SoftDelete;

class Coupon extends BaseModel
{

    use SoftDelete;

    

    // 表名
    protected $name = 'coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];

    /**
     * 优惠券活动列表
     */
    public function couponList($requestData, $user){
        $page = $requestData['page']??1;
        $limit = $requestData['limit']??10;
        $has_bao = $user['coupon_ids'] ? explode(',', $user['coupon_ids']) : [];
        $list = self::field('id,coupon_name,coupon_amount,type,createtime,coupon_time')->where(['is_delete' => 1, 'status' => 1])->order('createtime', 'desc')->page($page, $limit)->select()->toArray();
        if($list){
            foreach ($list as $key=>$value){
                $list[$key]['coupon_amount'] = floatval($value['coupon_amount']);
                $list[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
                $list[$key]['coupon_time'] = date('Y-m-d', $value['coupon_time']);
                if(in_array($value['id'], $has_bao)){
                    $list[$key]['read'] = 2;
                }else{
                    $list[$key]['read'] = 1;
                }
            }
        }
        return ['status' => 1, 'message' => '请求成功', 'result' => $list];
    }

    /**
     * 优惠券报名
     */
    public function couponJoin($requestData, $user){
        $coupon_id = $requestData['coupon_id']??0;
        $type = $requestData['type']??0;//1报名 2取消
        if(!$coupon_id || !$type || !in_array($type, [1,2])){
            return ['status' => 0, 'message' => '参数错误', 'result' => ''];
        }
        $coupon = self::field('id,coupon_name,coupon_amount,type,createtime,coupon_time')->where(['is_delete' => 1, 'status' => 1, 'id' => $coupon_id])->find();
        if(!$coupon){
            return ['status' => 0, 'message' => '优惠券不存在', 'result' => ''];
        }
        if($type == 1){
            $merchant_coupon_ids = $user['coupon_ids'] ? explode(',', $user['coupon_ids']) : [];
            if(in_array($coupon_id, $merchant_coupon_ids)){
                return ['status' => 0, 'message' => '该优惠券已报过名', 'result' => ''];
            }
            if(time() > $coupon['coupon_time']){
                return ['status' => 0, 'message' => '该优惠券活动已截止', 'result' => ''];
            }
        }else{
            $merchant_coupon_ids = $user['coupon_ids'] ? explode(',', $user['coupon_ids']) : [];
            if(!in_array($coupon_id, $merchant_coupon_ids)){
                return ['status' => 0, 'message' => '该优惠券已取消过报名', 'result' => ''];
            }
        }
        Db::startTrans();
        try {
            if($type == 1){
                if($user['coupon_ids']){
                    $new_coupon_ids = $user['coupon_ids'] . ',' . $coupon_id;
                }else{
                    $new_coupon_ids = $coupon_id;
                }
            }else{
                if($user['coupon_ids']){
                    $user_coupon_ids = explode(',', $user['coupon_ids']);
                    $key = array_search($coupon_id, $user_coupon_ids);
                    if ($key !== false){
                        array_splice($user_coupon_ids, $key, 1);
                    }
                    $new_coupon_ids = implode(',', $user_coupon_ids);
                }
            }

            $update = ['coupon_ids' => $new_coupon_ids, 'updatetime' => time()];
            $result = Merchant::where('id', $user['id'])->update($update);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status' => 1, 'message' => $e->getMessage(), 'result' => ''];
        }
        if($result){
            return ['status' => 1, 'message' => '请求成功', 'result' => ''];
        }else{
            return ['status' => 0, 'message' => '请求失败', 'result' => ''];
        }
    }

    //门槛类型处理
    public static function fetchLimitType($limit_type = null){
        $data =  [
            1 => '天数',
            2 => '金额',
        ];
        if (isset($data[$limit_type])) {
            return $data[$limit_type];
        }else{
            return $data;
        }
    }

    //类型处理
    public static function fetchType($type = null){
        $data =  [
            1 => '优惠券',
            2 => '折扣券',
            3 => '新人优惠券',
        ];
        if (isset($data[$type])) {
            return $data[$type];
        }else{
            return $data;
        }
    }
    





}
